
package com.hlkj.pay.controller.merchant;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.merchant.INoticeAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.merchant.MerchantNoticeDto;
import com.hlkj.pay.dto.merchant.NoticePageQueryDto;
import com.hlkj.pay.infrastructure.model.merchant.NoticeDO;
import com.hlkj.pay.service.merchant.INoticeService;
import com.hlkj.pay.vo.common.req.CommonUpdateStatusReq;
import com.hlkj.pay.vo.common.req.DeleteIdReq;
import com.hlkj.pay.vo.merchant.req.MerchantNoticePageQueryReq;
import com.hlkj.pay.vo.merchant.req.NoticePageQueryReq;
import com.hlkj.pay.vo.merchant.req.NoticeReq;
import com.hlkj.pay.vo.merchant.resp.MerchantNoticeResp;
import com.hlkj.pay.vo.merchant.resp.NoticeResp;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/11/06 16:26
 */
@Tag(name = "公告管理")
@RestController
@RequestMapping("/v1/notice")
@RequiredArgsConstructor
@Slf4j
public class NoticeController extends BaseController {

    private final INoticeAppService noticeAppService;

    private final INoticeService noticeService;

    @Operation(summary = "公告列表")
    @GetMapping("/page")
    public CommonResult<PageResult<NoticeResp>> queryNoticePage(NoticePageQueryReq noticePageQueryReq) {
        NoticePageQueryDto noticePageQueryDto = BeanUtil.copyProperties(noticePageQueryReq, NoticePageQueryDto.class);
        noticePageQueryDto.setLikeName(noticePageQueryReq.getName());
        noticePageQueryDto.setLikeTitle(noticePageQueryReq.getTitle());
        noticePageQueryDto.setLikePublisher(noticePageQueryReq.getPublisher());
        noticePageQueryDto.setPageNo(noticePageQueryReq.getPageNo());
        noticePageQueryDto.setPageSize(noticePageQueryReq.getPageSize());
        processQueryDate(noticePageQueryReq, noticePageQueryDto);
        PageResult<NoticeDO> pageResult = noticeService.queryPage(noticePageQueryDto);
        List<NoticeDO> list = pageResult.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(pageResult, Collections.emptyList());
        }
        List<NoticeResp> noticeResps = BeanUtil.copyToList(list, NoticeResp.class);
        // processMerchantList(noticeResps);
        return buildPageResult(pageResult, noticeResps);
    }

    @Operation(summary = "商户公告列表")
    @GetMapping("/merchant/page")
    public CommonResult<PageResult<MerchantNoticeResp>> queryNoticePage(MerchantNoticePageQueryReq merchantNoticePageQueryReq) {
        NoticePageQueryDto noticePageQueryDto = BeanUtil.copyProperties(merchantNoticePageQueryReq, NoticePageQueryDto.class);
        noticePageQueryDto.setLikeName(merchantNoticePageQueryReq.getName());
        noticePageQueryDto.setLikeTitle(merchantNoticePageQueryReq.getTitle());
        noticePageQueryDto.setPageNo(merchantNoticePageQueryReq.getPageNo());
        noticePageQueryDto.setPageSize(merchantNoticePageQueryReq.getPageSize());
        processQueryDate(merchantNoticePageQueryReq, noticePageQueryDto);
        PageResult<MerchantNoticeDto> pageResult = noticeService.queryMerchantPage(noticePageQueryDto);
        List<MerchantNoticeDto> list = pageResult.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(pageResult, Collections.emptyList());
        }
        List<MerchantNoticeResp> noticeResps = BeanUtil.copyToList(list, MerchantNoticeResp.class);
        processMerchantList(noticeResps);
        return buildPageResult(pageResult, noticeResps);
    }

    @Operation(summary = "公告详情")
    @GetMapping("/detail")
    public CommonResult<NoticeResp> detail(@RequestParam("id") Long id) {
        NoticeDO noticeDO = noticeService.queryById(id);
        if (noticeDO == null) {
            return CommonResult.success();
        }
        NoticeResp noticeResp = BeanUtil.copyProperties(noticeDO, NoticeResp.class);
        return CommonResult.success(noticeResp);
    }

    @Operation(summary = "商户查看公告详情")
    @GetMapping("/merchant/detail")
    public CommonResult<MerchantNoticeResp> merchantDetail(@RequestParam("id") Long id) {
        return noticeAppService.merchantDetail(id);
    }

    @Operation(summary = "新增公告")
    @PostMapping("")
    public CommonResult<String> addNotice(@Validated @RequestBody NoticeReq noticeReq) {
        return noticeAppService.addNotice(noticeReq);
    }

    @Operation(summary = "更新公告")
    @PutMapping("")
    CommonResult<Void> updateNotice(@Validated @RequestBody NoticeReq noticeReq) {
        return noticeAppService.updateNotice(noticeReq);
    }

    @Operation(summary = "删除公告")
    @DeleteMapping("")
    CommonResult<Void> deleteNotice(@RequestBody DeleteIdReq deleteIdReq) {
        return noticeAppService.deleteNotice(deleteIdReq.getId());
    }

    @Operation(summary = "更新状态")
    @PostMapping("/status")
    CommonResult<Void> updateStatus(@Validated @RequestBody CommonUpdateStatusReq commonUpdateStatusReq) {
        return noticeAppService.updateStatus(commonUpdateStatusReq);
    }

}
